<template>
  <div class="app-container">
    <sticky class="filter-container">
      <el-row :gutter="20">
        <el-col :span="21">
          <div class="block" style="float: left; margin-right: 3px">
            <el-date-picker
              v-model="datePicker"
              type="datetimerange"
              :picker-options="pickerOptions"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 248px"
              size="mini"
              @change="handleSearch"
            />
          </div>
          <el-input
            v-model="listSearch.search"
            placeholder="搜索"
            style="margin-right: 3px;width: 200px"
            class="filter-item"
            size="mini"
            clearable
            @keyup.enter.native="handleSearch"
            @clear="handleSearch"
            @blur="handleSearch"
          />
          <el-select
            v-model="listSearch.original"
            placeholder="原创"
            clearable
            class="filter-item"
            size="mini"
            style="margin-right: 10px;width: 80px"
            @change="handleSearch"
          >
            <el-option
              v-for="item in enabledTypeOptions"
              :key="item.key"
              :label="item.display_name"
              :value="item.key"
            />
          </el-select>
          <el-button
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleSearch"
          >
            搜索
          </el-button>
          <el-button
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-refresh-right"
            :loading="refreshLoading"
            @click="refresh"
          />
        </el-col>
        <el-col :span="3" style="text-align: right">
          <el-button
            v-if="checkPermission(['admin','outsideChain_all','outsideChain_create'])"
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="toDetail"
          >
            新增
          </el-button>
        </el-col>
      </el-row>
    </sticky>
    <el-table
      :data="list"
      border
      style="width: 100%"
    >
      <el-table-column
        label="ID"
        prop="id"
        width="55px"
      />
      <el-table-column
        label="标题"
        prop="title"
      />
      <el-table-column
        label="原创"
        prop="original"
        width="55px"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.original ? 'success':'danger'">{{ scope.row.original ? '是':'否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="作者"
        prop="author"
        width="100%"
      />
      <el-table-column
        label="公众号名称"
        prop="public_name"
      />
      <el-table-column
        label="公众号链接"
        prop="public"
      />
      <el-table-column
        prop="release"
        label="发布"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.release"
            active-text="是"
            inactive-text="否"
            @change="handleStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        prop="release_time"
        width="93%"
      />
      <el-table-column
        label="阅读"
        prop="reading"
        width="60%"
      />
      <el-table-column
        label="点赞"
        prop="like"
        width="60%"
      />
      <el-table-column
        label="在看"
        prop="look"
        width="60%"
      />
      <el-table-column
        label="广告主"
        prop="advertisers"
      />
      <el-table-column
        label="操作"
        width="120px"
        align="center"
      >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="预览" placement="top">
            <el-button
              v-if="checkPermission(['admin','outsideChain_all','outsideChain_list'])"
              type="text"
              icon="el-icon-view"
              @click="showView(scope.row.id)"
            />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button
              v-if="checkPermission(['admin','outsideChain_all','outsideChain_edit'])"
              type="text"
              icon="el-icon-edit"
              :disabled="scope.row.id === 1"
              @click="toDetail(scope.row)"
            />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button
              v-if="checkPermission(['admin','outsideChain_all','outsideChain_delete'])"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      style="text-align: right; margin-top: 20px"
      background
      :hide-on-single-page="true"
      :current-page="listSearch.page"
      :page-size="listSearch.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listSearch.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
// 按钮吸顶
import Sticky from '@/components/Sticky/index'
import waves from '@/directive/waves/waves'
import checkPermission from '@/utils/permission'

export default {
  components: { Sticky },
  directives: { waves },
  data() {
    return {
      datePicker: null,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      refreshLoading: false,
      listLoading: false,
      list: [],
      listSearch: {
        total: 0,
        page: 1,
        pageSize: 10,
        ordering: '-time',
        original: '',
        search: '',
        start_date: '',
        end_date: ''
      },
      enabledTypeOptions: [
        { key: 'true', display_name: '是' },
        { key: 'false', display_name: '否' }
      ],
      // 弹出框定义属性
      // （与子组件的 props 属性一致）dialog 控制新增、编辑、查看弹窗的显示与隐藏
      dialog: false,
      title: '',
      isedit: '',
      formid: 0
      // 弹出框定义属性
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // checkPermission 权限检查
    checkPermission,
    // 弹出框
    // 搜索
    handleSearch() {
      // 当页面不在第一页时搜索内容报错
      this.listSearch.page = 1
      this.dateFileter()
      this.getList()
    },
    dateFileter() {
      // 日期时间范围选择
      if (this.datePicker !== null) {
        this.listSearch.start_date = this.datePicker[0]
        this.listSearch.end_date = this.datePicker[1]
      } else {
        this.listSearch.start_date = ''
        this.listSearch.end_date = ''
      }
    },
    // 搜索
    // 列表
    getList() {
      this.list = [
        {
          id: '1000',
          title: '标题',
          original: true,
          release: true,
          author: '作者作者',
          public_name: '公众号名称',
          public: '这里是跳转链接',
          release_time: '2020-06-06',
          reading: '10万+',
          like: '10000',
          look: '10000',
          advertisers: '这里是广告主外跳链接'

        },
        {
          id: '1200',
          title: '标题',
          original: false,
          release: false,
          author: '作者',
          public_name: '公众号名称',
          public: '公众号',
          release_time: '发布时间',
          reading: '阅读',
          like: '点赞',
          look: '在看',
          advertisers: '广告主'

        }
      ]
    },
    // 列表
    // 排序
    sortChange(column) {
      const { prop, order } = column
      if (order === 'descending') {
        this.listSearch.ordering = `-${prop}`
      } else {
        this.listSearch.ordering = prop
      }
      this.handleSearch()
    },
    // 排序
    // 分页
    handleSizeChange(val) {
      this.listSearch.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listSearch.page = val
      this.getList()
    },
    // 分页
    // 刷新
    refresh() {
      this.refreshLoading = true
      this.getList()
      this.refreshLoading = false
      this.$notify.info({
        type: 'info',
        title: '提示',
        message: '已刷新',
        showClose: true,
        duration: 5 * 1000
      })
    },
    // 刷新
    // 详情页
    toDetail(row) {
      if (row.id) {
        this.did = row.id
      } else {
        this.did = 0
      }
      this.$router.push({
        path: 'detail',
        query: { id: this.did }
      })
    },
    // 详情页
    // 删除
    handleDelete(row) {
      console.log('删除')
    },
    // 删除
    // 是否发布
    handleStatus(row) {
      console.log('是否发布')
    }
    // 是否发布
  }
}
</script>

<style scoped>

</style>
